﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.IO;
using JshRCSWebApi.Util.FileHelper;

namespace JshRCSWebApi.Util.Log
{
    public class LogError
    {
        /// <summary>  
        /// 写入日志到文本文件  
        /// </summary>  
        /// <param name="action">接口</param>
        /// <param name="strMessage">日志内容</param>
        public static void WriteTextLog(string funname, string errmsg)
        {
            try
            {
                string path = SysConfig.Params.Log.LogErrorPath + @"Log\" + DateTime.Now.ToString("yyyy-MM") + @"\";
                if (!Directory.Exists(path))
                    Directory.CreateDirectory(path);

                string fileFullPath = path + DateTime.Now.ToString("yyyy-MM-dd") + ".error.txt";
                StringBuilder str = new StringBuilder();
                str.Append("Time:    " + DateTime.Now.ToString() + "\r\n");
                str.Append("Action:  " + funname + "\t" + errmsg + "\r\n");
                //str.Append("-----------------------------------------------------------\r\n\r\n");
                StreamWriter sw;
                if (!System.IO.File.Exists(fileFullPath))
                {
                    sw = System.IO.File.CreateText(fileFullPath);
                }
                else
                {
                    sw = System.IO.File.AppendText(fileFullPath);
                }
                sw.WriteLine(str.ToString());
                sw.Close();
            }
            catch { }
        }

        /// <summary>
        /// 写入日志到文本文件  
        /// </summary>
        /// <param name="funname">方法名</param>
        /// <param name="errmsg">错误内容</param>
        /// <param name="ls_filename">文件名</param>
        public static void WriteTextLog(string funname, string errmsg, string ls_filename)
        {
            try
            {
                string path = SysConfig.Params.Log.LogErrorPath + @"Log\" + DateTime.Now.ToString("yyyy-MM") + @"\";
                if (!Directory.Exists(path))
                    Directory.CreateDirectory(path);

                string fileFullPath = path + DateTime.Now.ToString("yyyy-MM-dd") + "." + ls_filename + ".txt";
                StringBuilder str = new StringBuilder();
                str.Append("Time:    " + DateTime.Now.ToString() + "\r\n");
                str.Append("Action:  " + funname + "\t" + errmsg + "\r\n");
                //str.Append("-----------------------------------------------------------\r\n\r\n");
                StreamWriter sw;
                if (!System.IO.File.Exists(fileFullPath))
                {
                    sw = System.IO.File.CreateText(fileFullPath);
                }
                else
                {
                    sw = System.IO.File.AppendText(fileFullPath);
                }
                sw.WriteLine(str.ToString());
                sw.Close();
            }
            catch { }
        }

        /// <summary>
        ///  写入日志到文本文件
        /// </summary>
        /// <param name="funname">方法名</param>
        /// <param name="errmsg">错误内容</param>
        /// <param name="ls_filename">文件名</param>
        /// <param name="ls_floder">子文件夹名</param>
        public static void WriteTextLog(string funname, string errmsg, string ls_filename, string ls_floder)
        {
            try
            {
                string path = SysConfig.Params.Log.LogErrorPath + @"Log\" + DateTime.Now.ToString("yyyy-MM") + @"\" + ls_floder + @"\";
                if (!Directory.Exists(path))
                    Directory.CreateDirectory(path);

                string fileFullPath = path + DateTime.Now.ToString("yyyy-MM-dd") + "." + ls_filename + ".txt";
                StringBuilder str = new StringBuilder();
                str.Append("Time:    " + DateTime.Now.ToString() + "\r\n");
                str.Append("Action:  " + funname + "\t" + errmsg + "\r\n");
                //str.Append("-----------------------------------------------------------\r\n\r\n");
                StreamWriter sw;
                if (!System.IO.File.Exists(fileFullPath))
                {
                    sw = System.IO.File.CreateText(fileFullPath);
                }
                else
                {
                    sw = System.IO.File.AppendText(fileFullPath);
                }
                sw.WriteLine(str.ToString());
                sw.Close();
            }
            catch { }
        }
    }
}
